import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
} from '@nestjs/common';
import { FeishuService } from './feishu.service';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { CreateTenantAccessToken } from './dto/create-tenant-access-token.dto';
import { QueryFeiShuTenantAccessTokenType } from './dto/query-feishu-tenant-access-token.dto';
import { GetBatchOpenIdByPhoneDto } from './dto/get-batch-openId-by-phone.dto';
import {
  SendCardMessagesToUserDto,
  SendMessagesToUserDto,
} from './dto/send-messages-to-user.dto';

@Controller('feishu')
@ApiTags('飞书')
export class FeishuController {
  constructor(private readonly feishuService: FeishuService) {}

  @Get('/sup-adapter/feishu/sync/openid')
  @ApiOperation({ summary: '小平台同步用户的 OpenID 字段' })
  obpSyncOpenid() {
    return this.feishuService.obpSyncOpenid();
  }

  @Get('/db/tenants/getTenantAccessToken/:tenant')
  @ApiOperation({ summary: '从数据库获取指定应用的AccessToken' })
  getTenantAccessTokenFromDB(
    @Param('tenant') tenant: QueryFeiShuTenantAccessTokenType,
  ) {
    return this.feishuService.getTenantAccessTokenFromDB(tenant);
  }

  @Post('/db/tenants/addTenantAccessToken')
  @ApiOperation({ summary: '向数据库写入指定应用的AccessToken' })
  addTenantAccessTokenToDb(@Body() createDto: CreateTenantAccessToken) {
    return this.feishuService.addTenantAccessTokenToDb(createDto);
  }

  @Get('/open-apis/tenants/getTenantAccessToken/:tenant')
  @ApiOperation({ summary: '从飞书开发平台获取指定应用的AccessToken' })
  getTenantAccessTokenFromOrigin(
    @Param('tenant') tenant: QueryFeiShuTenantAccessTokenType,
  ) {
    return this.feishuService.getTenantAccessTokenFromOrigin(tenant);
  }

  @Post('/open-apis/users/getBatchOpenIdByPhone')
  @ApiOperation({ summary: '从飞书开发平台通过手机号或邮箱获取用户 ID' })
  getBatchOpenIdByPhone(@Body() dto: GetBatchOpenIdByPhoneDto) {
    return this.feishuService.getBatchOpenIdByPhone(dto);
  }

  @Post('/open-apis/im/messages')
  @ApiOperation({ summary: '给指定用户发送普通文字消息' })
  sendMessagesToUser(@Body() dto: SendMessagesToUserDto) {
    return this.feishuService.sendMessagesToUser(dto);
  }

  @Post('/open-apis/im/messages/card')
  @ApiOperation({ summary: '给指定用户发送卡片消息' })
  sendCardMessagesToUser(@Body() dto: SendCardMessagesToUserDto) {
    return this.feishuService.sendMessagesToUser(dto);
  }
}
